home *** CD-ROM | disk | FTP | other *** search
/ AppleScript - The Beta Release / AppleScript - The Beta Release.iso / Documentation / develop / Apple Event Objects and You / Apple Event Objects (code) / Menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-08  |  7.1 KB  |  261 lines  |  [TEXT/KAHL]

  1. /* File: Menus.c */
  2.  
  3. #include <AppleEvents.h>
  4. #include "AERegistry.h"
  5. #include "AEObjects.h"
  6. #include "AEPackObject.h"
  7. #include "ScriptScrap.h"
  8. #include "Prototypes.h"
  9.  
  10.  
  11.  
  12. void SetItemEnable (MenuHandle theMenu, short theItem, Boolean enabled)
  13. {
  14.     if (enabled)
  15.         EnableItem(theMenu, theItem);
  16.     else
  17.         DisableItem(theMenu, theItem);
  18. } /* SetItemEnable */
  19.  
  20.  
  21. void EnableFileCommands ()
  22. {
  23.     MenuHandle    fileMenu = GetMHandle(kFileMenu);
  24.     WindowPtr    inFront = FrontWindow();
  25.  
  26.     EnableItem (fileMenu, cNew);
  27.     EnableItem (fileMenu, cOpen);
  28.     SetItemEnable(fileMenu, cClose, (inFront != NULL) && (((WindowPeek)inFront)->goAwayFlag));
  29. } /* EnableFileCommands */
  30.  
  31.  
  32. void AdjustMenus(void)
  33. {
  34.     WindowPtr    inFront = FrontWindow();
  35.     Boolean        isAUserWindow, isDeskAcc;
  36.  
  37.     EnableFileCommands();
  38.     DisableItem(GetMHandle(kEditMenu), 0);
  39.     if ((inFront) && (((WindowPeek)inFront)->windowKind < 0))
  40.         EnableItem(GetMHandle(kEditMenu), 0);
  41.     else
  42.         DisableItem(GetMHandle(kEditMenu), 0);
  43. } /* AdjustMenus */
  44.  
  45.  
  46.  
  47. void DoAppleCmds (short theItem)
  48. {
  49.     Str255        name;            /* string for DA name    */
  50.  
  51.     switch (theItem) {
  52.  
  53.         case cAbout: 
  54.             Alert(kAboutDialog, NULL);
  55.             break;
  56.             
  57.         default:
  58.             GetItem(GetMHandle(kAppleMenu), theItem, (StringPtr)&name);
  59.             OpenDeskAcc((StringPtr)&name);
  60.     }
  61. }
  62.  
  63.  
  64. void DoFileCmds (short theItem)
  65. {
  66.     long        result = 0;
  67.     FSSpec        theFile;
  68.     SFTypeList     typeList = {'scbk', '    ', '    ', '    '};
  69.     StandardFileReply     reply;
  70.     OSErr        err;
  71.     
  72.     switch (theItem) {
  73.  
  74.         case cNew: 
  75.             ae_do_NewDocument();
  76.         break;
  77.         
  78.         case cOpen: 
  79.             StandardGetFile(NULL, 1, typeList, &reply);
  80.             if (reply.sfGood)
  81.                 ae_do_Open(&reply.sfFile);
  82.         break;
  83.                 
  84.         case cClose: 
  85.             ae_do_Close();
  86.         break;
  87.  
  88.         case cQuit: 
  89.             ae_do_Quit();
  90.     }
  91. } /* DoFileCmds */
  92.  
  93.  
  94. void Dispatch (long menuResult)
  95. {
  96.     short     theMenu = (menuResult >> 16);                    /* menu selected    */
  97.     short     theItem = (menuResult & 0x0000FFFF);               /* item selected    */
  98.  
  99.     switch (theMenu) {
  100.  
  101.         case kAppleMenu: 
  102.             DoAppleCmds(theItem);
  103.             break;
  104.             
  105.         case kFileMenu: 
  106.             DoFileCmds(theItem);
  107.             break;
  108.             
  109.         case kEditMenu: 
  110.             SystemEdit(theItem - 1);
  111.             break;
  112.     }
  113.     HiliteMenu(0);               /* un-hilite selected menu */
  114. }
  115.  
  116.  
  117. void ae_do_Quit (void)
  118. {
  119.     /* Construct an Apple event to quit the application */
  120.  
  121.     OSErr            err = noErr;
  122.     AppleEvent        anAppleEvent, replyAE;
  123.     DescType        saveOption;
  124.     
  125.     err = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &gSelfAddressDesc,
  126.                              kAutoGenerateReturnID, kAnyTransactionID,
  127.                              &anAppleEvent);
  128.     if (err == noErr) {
  129.         /* Send the event to ourselves */
  130.         err = AESend (&anAppleEvent, &replyAE, kAENoReply + kAECanInteract,
  131.                       kAENormalPriority, kAEDefaultTimeout, (IdleProcPtr)0L,
  132.                       (EventFilterProcPtr)0L);
  133.     
  134.     }
  135.     (void)AEDisposeDesc(&anAppleEvent);
  136.     CheckResult(err, "ae_do_Quit");
  137. }
  138.  
  139.  
  140. void ae_do_Close ()
  141. {
  142.     /* Construct an Apple event to close the frontmost window */
  143.     /* The direct parameter is an object specifier for the window to be closed */
  144.  
  145.     OSErr            err = noErr;
  146.     AppleEvent        anAppleEvent, replyAE;
  147.     AEDesc            objSpec,
  148.                     nullContainer = {'null', 0L},
  149.                     keyData;
  150.     long            index;
  151.  
  152.     if (FrontWindow() == NULL) return;     /* Don't do the following if no windows are open */
  153.  
  154.     err = AECreateAppleEvent(kAECoreSuite, kAEClose, &gSelfAddressDesc,
  155.                              kAutoGenerateReturnID, kAnyTransactionID,
  156.                              &anAppleEvent);
  157.     if (err != noErr) goto done;
  158.  
  159.     /* Create an object specifier for the frontmost window */
  160.     index = 1; /* This is the frontmost window or document */
  161.     (void)AECreateDesc(typeLongInteger, (Ptr)&index, sizeof(index), &keyData);
  162.     err = CreateObjSpecifier(cWindow, &nullContainer, formAbsolutePosition, &keyData, true, &objSpec);    /* This will dispose of the inputs for us */
  163.     if (err != noErr) goto done;
  164.     
  165.     /* Put this object specifier into the direct parameter */
  166.     err = AEPutParamDesc (&anAppleEvent, keyDirectObject, &objSpec);    /* The object to be closed */
  167.     (void)AEDisposeDesc (&objSpec);
  168.     if (err != noErr) goto done;
  169.  
  170.     /* Send the event to ourselves */
  171.     err = AESend (&anAppleEvent, &replyAE, kAENoReply,
  172.                   kAENormalPriority, kAEDefaultTimeout, (IdleProcPtr)0L,
  173.                   (EventFilterProcPtr)0L);
  174.     
  175. done:
  176.     (void)AEDisposeDesc(&anAppleEvent);
  177.     CheckResult(err, "ae_do_Close");
  178. }
  179.  
  180. void ae_do_Open (FSSpec *theFileSpec)
  181. {
  182.     OSErr    err;
  183.     AppleEvent    anAppleEvent, replyAE;
  184.     AEDesc        aliasDesc = {typeNull, NULL};
  185.     
  186.     /* Send an "open document {file-alias}" event back to this application */
  187.     err = AECreateAppleEvent(kCoreEventClass, /* <- Note: this is what AppleEvents.h calls the 4required suite */
  188.                              kAEOpenDocuments, &gSelfAddressDesc,
  189.                              kAutoGenerateReturnID, kAnyTransactionID,
  190.                              &anAppleEvent);
  191.     if (err != noErr) goto done;
  192.     /* Make an alias for the file */
  193.     err = NewAlias(NULL, theFileSpec, (AliasHandle*)&aliasDesc.dataHandle);
  194.     if (err != noErr) goto done;
  195.     aliasDesc.descriptorType = typeAlias;
  196.     
  197.     /* Add it to the event */
  198.     err = AEPutParamDesc(&anAppleEvent, keyDirectObject, &aliasDesc);    /* The file to be opened */
  199.     if (err != noErr) goto done;
  200.     err = AESend (&anAppleEvent, &replyAE, kAENoReply,
  201.                   kAENormalPriority, kAEDefaultTimeout, (IdleProcPtr)0L,
  202.                   (EventFilterProcPtr)0L);
  203.  
  204. done:
  205.     if (aliasDesc.dataHandle != NULL)
  206.         (void) AEDisposeDesc(&aliasDesc);
  207.     (void) AEDisposeDesc(&anAppleEvent);
  208.     CheckResult(err, "ae_do_Open");
  209. }
  210.  
  211. void ae_do_NewDocument ()
  212. {
  213.     /* Code revised 1/22/92 to match the Winter '92 Registry */
  214.     AppleEvent        anAppleEvent = {typeNull, NULL}, replyAE;
  215.     AERecord        scratchRecord = {typeNull, NULL};
  216.     AERecord        insertionLoc = {typeNull, NULL};
  217.     AEDesc            nullDesc = { typeNull, NULL };
  218.     DescType        scratchType;
  219.     OSErr            err = noErr;
  220.  
  221.     /* Send a "Create Element {document from null}" event back to this application */
  222.     err = AECreateAppleEvent(kAECoreSuite, kAECreateElement, &gSelfAddressDesc,
  223.                              kAutoGenerateReturnID, kAnyTransactionID,
  224.                              &anAppleEvent);
  225.     CheckResult(err, "ae_do_NewDocument: New, creating Apple event");
  226.     
  227.     /* Create the typeInsertionLoc specifier, which consists of an object specifier and */
  228.     /* a position within the container ("null", and "at the beginning", respectively.)    */
  229.     err = AECreateList(NULL, 0, true /* isRecord */, &scratchRecord);
  230.     if (err != noErr) goto done;
  231.     err = AEPutParamDesc(&scratchRecord, keyAEObject, &nullDesc);
  232.     if (err != noErr) goto done;
  233.     scratchType = kAEBeginning;
  234.     err = AEPutParamPtr (&scratchRecord, keyAEPosition, typeEnumeration, (Ptr)&scratchType, sizeof(scratchType));
  235.     if (err != noErr) goto done;
  236.     err = AECoerceDesc(&scratchRecord, typeInsertionLoc, &insertionLoc);
  237.     if (err != noErr) goto done;
  238.     
  239.     /* Place the InsertionLoc into the Apple event */
  240.     err = AEPutParamDesc(&anAppleEvent, keyAEInsertHere, &insertionLoc);    /* Where to put the window */
  241.     if (err != noErr) goto done;
  242.     
  243.     scratchType = cDocument;
  244.     (void)AEPutParamPtr(&anAppleEvent, keyAEObjectClass, typeType, (Ptr)&scratchType, sizeof(scratchType));    /* cWindow */
  245.  
  246.     err = AESend (&anAppleEvent, &replyAE, kAENoReply,
  247.                   kAENormalPriority, kAEDefaultTimeout, (IdleProcPtr)0L,
  248.                   (EventFilterProcPtr)0L);
  249.  
  250. done:
  251.     if (scratchRecord.dataHandle != NULL)
  252.         (void)AEDisposeDesc(&scratchRecord);
  253.     if (insertionLoc.dataHandle != NULL)
  254.         (void)AEDisposeDesc(&insertionLoc);
  255.     if (insertionLoc.dataHandle != NULL)
  256.         (void)AEDisposeDesc(&anAppleEvent);
  257.  
  258.     CheckResult(err, "ae_do_NewDocument");
  259. }
  260.  
  261.